<?php namespace App\Http\Controllers\Api;

use App\Http\Requests;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Area;
use Illuminate\Contracts\Validation\Validator;

class AreaController extends Controller
{
    public function getAreas(Request $request)
    {
        $areas = [['value' => '', 'label' => '']];
        if (is_numeric($id = $request->input('id'))) {
            $level = 1;
            if ($area = Area::find($id)) {
                $level += $area->level;
            }
            $where = ['p_id' => $id, 'level' => $level];
            $areas = array_merge($areas, Area::where($where)->get(['id as value', 'name as label'])->toArray());
        }
        return response()->json($areas);
    }
}
